package com.fe.class03;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @Description 有限长度数组实现队列
 * @Author sosuke :-)
 * @Date 2021/12/11 06:43
 */
public class Code06_RingArrayToQueue {
    public static class MyQueue {
        private int[] arr;
        private int pushi; // 加数的位置
        private int popi; // 取数的位置
        private int size; // 用于控制队列能否进出
        private final int limit;

        public MyQueue(int limit) {
            this.limit = limit;
            arr = new int[limit];
            pushi = 0;
            popi = 0;
            size = 0;
        }

        public boolean isEmpty() {
            return size == 0;
        }

        public boolean isFull() {
            return size >= limit;
        }

        public void offer(int data) {
            if (isFull()) {
                throw new RuntimeException("队列满了，不能再加了");
            }
            size++;
            arr[pushi] = data;
            pushi = nextIndex(pushi);
        }

        public int poll() {
            if (isEmpty()) {
                throw new RuntimeException("队列空了，不能再取了");
            }
            size--;
            int ans = arr[popi];
            popi = nextIndex(popi);
            return ans;
        }

        // 返回下一个下标，循环的
        private int nextIndex(int i) {
            return i < limit - 1 ? i + 1 : 0;
        }

        public static void main(String[] args) {
            int testTimes = 10000;
            int maxValue = 100;
            int maxSize =  15;
            for (int i = 0; i < testTimes; i++) {
                int size = (int) (Math.random() * (maxSize + 1));
                MyQueue myQueue = new MyQueue(size);
                Queue<Integer> queue = new LinkedList<>();
                for (int j = 0; j < size; j++) {
                    int numQueue = (int) (Math.random() * maxValue);
                    if (queue.isEmpty()) {
                        queue.offer(numQueue);
                        myQueue.offer(numQueue);
                    } else {
                        if (Math.random() < 0.5) {
                            queue.offer(numQueue);
                            myQueue.offer(numQueue);
                        } else {
                            if (!isEqual(queue.poll(), myQueue.poll())) {
                                System.out.println("MyQueue oops");
                            }
                        }
                    }
                }
            }
            System.out.println("Nice");
        }

        // public static void main(String[] args) {
        //     MyQueue myQueue = new MyQueue(5);
        //     myQueue.push(1);
        //     myQueue.push(2);
        //     myQueue.push(3);
        //     myQueue.push(4);
        //     myQueue.push(5);
        //     System.out.println(myQueue.poll());
        //     System.out.println(myQueue.poll());
        //     System.out.println(myQueue.poll());
        //     System.out.println(myQueue.poll());
        //     System.out.println(myQueue.poll());
        //     System.out.println(myQueue.poll());
        // }

        private static boolean isEqual(Integer o1, Integer o2) {
            if (null == o1 && null != o2) return false;
            if (null != o1 && null == o2) return false;
            if (null == o1) return true;
            return o1.equals(o2);
        }
    }
}
